package com.ciwong.ciwongwrite.widget;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;

import com.ciwong.ciwongwrite.R;


public abstract class DrawAction
{

    public static final String DRAW_POINT = "DRAW_POINT";

    public static final String DRAW_PATH = "DRAW_PATH";

    public static final String DRAW_LINE = "DRAW_LINE";

    public static final String DRAW_RECT = "DRAW_RECT";

    public static final String DRAW_FILL_RECT = "DRAW_FILL_RECT";

    public static final String DRAW_CIRCLE = "DRAW_CIRCLE";

    public static final String DRAW_FILL_CIRCLE = "DRAW_FILL_CIRCLE";

    public static final String DRAW_ERASER = "DRAW_ERASER";

    public static final String DRAW_BITMAP = "DRAW_BITMAP";

    public static final int DRAW_PATH_NO = 0;

    public static final int DRAW_POINT_NO = 1;

    public static final int DRAW_LINE_NO = 2;

    public static final int DRAW_RECT_NO = 3;

    public static final int DRAW_FILL_RECT_NO = 4;

    public static final int DRAW_CIRCLE_NO = 5;

    public static final int DRAW_FILL_CIRCLE_NO = 6;

    public static final int DRAW_ERASER_NO = 7;

    public static final int DRAW_BITMAP_NO = 8;

    private static final int SCALE_SIZE = 5;

    private static final int APLHA = 80;

    private static int index;

    protected int color;

    protected int size;

    protected int flag;

    protected float startX;

    protected float startY;

    protected float stopX;

    protected float stopY;

    protected Paint mPaint = new Paint();

    public DrawAction()
    {
        color = Color.BLACK;
    }

    public DrawAction(int color)
    {
        this.color = color;
        flag = index;
        index++;
    }

    public int getFlag()
    {
        return flag;
    }

    public void setColor(int color)
    {
        this.color = color;
        mPaint.setColor(color);
    }

    public void setAplha(int color)
    {
        int r = Color.red(color);
        int g = Color.green(color);
        int b = Color.blue(color);
        mPaint.setColor(Color.argb(APLHA, r, g, b));
    }

    public void setSize(int size)
    {
        this.size = size;
        mPaint.setStrokeWidth(size);
    }

    public abstract void draw(Canvas canvas);

    public abstract void move(float mx, float my);

    public void large()
    {
        startX -= SCALE_SIZE;
        startY -= SCALE_SIZE;
        stopX += SCALE_SIZE;
        stopY += SCALE_SIZE;
        setAplha(color);
    }

    public void reset()
    {
        startX += SCALE_SIZE;
        startY += SCALE_SIZE;
        stopX -= SCALE_SIZE;
        stopY -= SCALE_SIZE;
        setColor(color);
    }

    public void moveTo(float mx, float my)
    {
        startX -= mx;
        startY -= my;
        stopX -= mx;
        stopY -= my;
    }

    /**
     * 坐标是否在该区域
     *
     * @param x
     *            x
     * @param y
     *            y
     */
    public abstract boolean isInArea(float x, float y);

    /**
     * 计算圆的半径
     *
     * @param sx
     *            起点X
     * @param sy
     *            起点Y
     * @param ex
     *            终点X
     * @param ey
     *            终点Y
     * @return radius
     */
    public static double calcCircleRadius(float sx, float sy, float ex, float ey)
    {
        final float mx = Math.abs(sx - ex);
        final float my = Math.abs(sy - ey);
        return Math.sqrt(mx * mx + my * my);
    }

    public static class DrawPoint extends DrawAction
    {
        float x;

        float y;

        public DrawPoint(float px, float py, int size, int color)
        {
            super(color);
            this.x = px;
            this.y = py;
            setSize(size);
            setColor(color);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawCircle(x, y, size, mPaint);
        }

        @Override
        public void move(float mx, float my)
        {
            x = mx;
            y = my;
        }

        @Override
        public void moveTo(float mx, float my)
        {
            x -= mx;
            y -= my;
        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return calcCircleRadius(this.x, this.y, x, y) <= size;
        }

        @Override
        public void reset()
        {
            size -= SCALE_SIZE;
            setColor(color);
        }

        @Override
        public void large()
        {
            size += SCALE_SIZE;
            setAplha(color);
        }
    }

    public static class DrawPath extends DrawAction
    {
        private static final float TOUCH_TOLERANCE = 4;

        Path path = new Path();

        float mX;

        float mY;

        public DrawPath()
        {
            super();
            size = 1;
            init();
        }

        public DrawPath(float x, float y, int size, int color)
        {
            super(color);
            this.size = size;
            path.moveTo(x, y);
            path.lineTo(x, y);
            mX = x;
            mY = y;
            init();
        }

        private void init()
        {
            setColor(color);
            setSize(size);
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setStyle(Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
        }

        @Override
        public void draw(Canvas canvas)
        {
            canvas.drawPath(path, mPaint);
        }

        @Override
        public void move(float x, float y)
        {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
            {
                path.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
                mX = x;
                mY = y;
            }
        }

        @Override
        public void moveTo(float mx, float my)
        {

        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return false;
        }

    }

    public static class DrawLine extends DrawAction
    {

        public DrawLine()
        {
            super();
            startX = 0;
            startY = 0;
            stopX = 0;
            stopY = 0;
            init();
        }

        public DrawLine(float x, float y, int size, int color)
        {
            super(color);
            startX = x;
            startY = y;
            stopX = x;
            stopY = y;
            this.size = size;
            init();
        }

        private void init()
        {
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Style.STROKE);
            mPaint.setColor(color);
        }

        public void draw(Canvas canvas)
        {
            mPaint.setStrokeWidth(size);
            canvas.drawLine(startX, startY, stopX, stopY, mPaint);
        }

        public void move(float mx, float my)
        {
            stopX = mx;
            stopY = my;
        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return false;
        }

    }

    public static class DrawRect extends DrawAction
    {
        public DrawRect()
        {
            super();
            startX = 0;
            startY = 0;
            stopX = 0;
            stopY = 0;
            init();
        }

        public DrawRect(float x, float y, int size, int color)
        {
            super(color);
            startX = x;
            startY = y;
            stopX = x;
            stopY = y;
            this.size = size;
            init();
        }

        private void init()
        {
            mPaint.setAntiAlias(true);
            mPaint.setStyle(Style.STROKE);
            mPaint.setColor(color);
            mPaint.setStrokeWidth(size);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawRect(startX, startY, stopX, stopY, mPaint);
        }

        public void move(float mx, float my)
        {
            stopX = mx;
            stopY = my;
        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return x > startX && x < stopX && y > startY && y < stopY;
        }

    }

    public static class DrawFillRect extends DrawRect
    {

        public DrawFillRect()
        {
            super();
            init();
        }

        public DrawFillRect(float x, float y, int size, int color)
        {
            super(x, y, size, color);
            init();
        }

        private void init()
        {
            mPaint.setStyle(Style.FILL);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawRect(startX, startY, stopX, stopY, mPaint);
        }

        public void move(float mx, float my)
        {
            stopX = mx;
            stopY = my;
        }

    }

    public static class DrawCircle extends DrawAction
    {
        protected float radius;

        public DrawCircle()
        {
            super();
            startX = 0;
            startY = 0;
            stopX = 0;
            stopY = 0;
            radius = 0;
            init();
        }

        public DrawCircle(float x, float y, int size, int color)
        {
            super(color);
            startX = x;
            startY = y;
            stopX = x;
            stopY = y;
            radius = 0;
            this.size = size;
            init();
        }

        private void init()
        {
            mPaint.setAntiAlias(true);
            mPaint.setColor(color);
            mPaint.setStrokeWidth(size);
            mPaint.setStyle(Style.STROKE);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawCircle((startX + stopX) / 2, (startY + stopY) / 2,
                    radius, mPaint);
        }

        public void move(float mx, float my)
        {
            stopX = mx;
            stopY = my;
            radius = (float) ((Math.sqrt((mx - startX) * (mx - startX)
                    + (my - startY) * (my - startY))) / 2);
        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return calcCircleRadius((startX + stopX) / 2, (startY + stopY) / 2,
                    x, y) <= radius;
        }

        @Override
        public void reset()
        {
            radius -= SCALE_SIZE;
            setColor(color);
        }

        @Override
        public void large()
        {
            radius += SCALE_SIZE;
            setAplha(color);
        }

    }

    public static class DrawFillCircle extends DrawCircle
    {

        public DrawFillCircle()
        {
            super();
            init();
        }

        public DrawFillCircle(float x, float y, int size, int color)
        {
            super(x, y, size, color);
            init();
        }

        private void init()
        {
            mPaint.setStyle(Style.FILL);
        }

        public void draw(Canvas canvas)
        {
            canvas.drawCircle((startX + stopX) / 2, (startY + stopY) / 2,
                    radius, mPaint);
        }

        public void move(float mx, float my)
        {
            stopX = mx;
            stopY = my;
            radius = (float) ((Math.sqrt((mx - startX) * (mx - startX)
                    + (my - startY) * (my - startY))) / 2);
        }

    }

    public static class DrawEraser extends DrawAction
    {
        Path path = new Path();

        Paint eraserPaint;

        float x;

        float y;

        public DrawEraser()
        {
            super();
            size = 1;
            init();
        }

        public DrawEraser(float x, float y, int size, int color)
        {
            super(color);
            this.size = size;
            this.x = x;
            this.y = y;
            path.moveTo(x, y);
            path.lineTo(x, y);
            init();
        }

        private void init()
        {
            mPaint.setAntiAlias(true);
            mPaint.setDither(true);
            mPaint.setStrokeWidth(size);
            mPaint.setStyle(Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.SQUARE);
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
            eraserPaint = new Paint();
            eraserPaint.setAntiAlias(true);
            eraserPaint.setColor(Color.GRAY);
            eraserPaint.setStyle(Style.FILL);
        }

        @Override
        public void draw(Canvas canvas)
        {
            canvas.drawPath(path, mPaint);
            canvas.drawCircle(x, y, size, eraserPaint);
        }

        public void clearPoint(Canvas canvas)
        {
            canvas.drawPath(path, mPaint);
        }

        public void move(float mx, float my)
        {
            path.lineTo(mx, my);
            x = mx;
            y = my;
        }

        @Override
        public boolean isInArea(float x, float y)
        {
            return false;
        }
    }

    public static class DrawBitmap extends DrawAction
    {
        private static final int CROP_EDGE = 20;

        Bitmap mBitmap;

        Bitmap cropBitmap;

        Bitmap primaryBitmap;

        ArrayList<CropImage> cropImages;

        Paint cropPaint;

        Context mContext;

        CropImage curCropImage;

        int editFlag;

        boolean isCrop;

        int w;

        int h;

        int cropW, cropH, cropBitmapW, cropBitmapH;

        Paint alphaPaint;

        public DrawBitmap(float x, float y, Bitmap mBitmap, Context context)
        {
            super();
            this.mBitmap = mBitmap;
            primaryBitmap = mBitmap;
            w = mBitmap.getWidth();
            h = mBitmap.getHeight();
            startX = x;
            startY = y;
            cropW = cropH = 0;
            mContext = context;
            editFlag = CropImage.FLAG_NONE;
            alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            alphaPaint.setAlpha(0xaa);
        }

        @Override
        public void draw(Canvas canvas)
        {
            canvas.drawBitmap(mBitmap, startX, startY, isCrop ? alphaPaint
                    : null);
            if (isCrop)
            {
                canvas.drawBitmap(cropBitmap, startX - CROP_EDGE / 2 - cropW
                        / 2, startY - CROP_EDGE / 2 - cropH / 2, null);
            }
        }

        @Override
        public void move(float mx, float my)
        {
            startX = mx;
            startY = my;
        }

        /**
         * 在画布中移动图片
         */
        @Override
        public void moveTo(float mx, float my)
        {
            if (editFlag == CropImage.FLAG_NONE)
            {
                startX -= mx;
                startY -= my;
            }
            else
            {
                final int bw = mBitmap.getWidth();
                final int bh = mBitmap.getHeight();
                switch (editFlag)
                {
                    case CropImage.FLAG_LEFT:
                        mBitmap = Bitmap.createBitmap(valid(bw + (int) mx), bh,
                                Config.ARGB_8888);
                        startX -= mx;
                        break;
                    case CropImage.FLAG_TOP:
                        mBitmap = Bitmap.createBitmap(bw, valid(bh + (int) my),
                                Config.ARGB_8888);
                        startY -= my;
                        break;
                    case CropImage.FLAG_BOTTOM:
                        mBitmap = Bitmap.createBitmap(bw, valid(bh - (int) my),
                                Config.ARGB_8888);
                        break;
                    case CropImage.FLAG_RIGHT:
                        mBitmap = Bitmap.createBitmap(valid(bw - (int) mx), bh,
                                Config.ARGB_8888);
                        break;
                    case CropImage.FLAG_LEFT_TOP:
                        mBitmap = Bitmap.createBitmap(valid(bw + (int) mx),
                                valid(bh + (int) my), Config.ARGB_8888);
                        startX -= mx;
                        startY -= my;
                        break;
                    case CropImage.FLAG_RIGHT_TOP:
                        mBitmap = Bitmap.createBitmap(valid(bw - (int) mx),
                                valid(bh + (int) my), Config.ARGB_8888);
                        startY -= my;
                        break;
                    case CropImage.FLAG_LEFT_BOTTOM:
                        mBitmap = Bitmap.createBitmap(valid(bw + (int) mx),
                                valid(bh - (int) my), Config.ARGB_8888);
                        startX -= mx;
                        break;
                    case CropImage.FLAG_RIGHT_BOTTOM:
                        mBitmap = Bitmap.createBitmap(valid(bw - (int) mx),
                                valid(bh - (int) my), Config.ARGB_8888);
                        break;
                    default:
                        break;
                }
                final Canvas mCanvas = new Canvas(mBitmap);
                Rect dst = new Rect(0, 0, mBitmap.getWidth(),
                        mBitmap.getHeight());
                mCanvas.drawBitmap(primaryBitmap, null, dst, null);
                createCropBitmap();
            }
        }

        /**
         * 验证图片的宽高不能小于指定值
         *
         * @param num
         *            宽、高
         * @return 正确的宽、高
         */
        private int valid(int num)
        {
            return num > 5 ? num : 5;
        }

        /**
         * 使图片放大，说明该图片已被选中
         */
        @Override
        public void large()
        {
            if (cropImages == null)
            {
                cropImages = new ArrayList<CropImage>();
                cropPaint = new Paint();
                cropPaint.setAntiAlias(true);
                cropPaint.setStrokeWidth(5);
                cropPaint.setColor(0x88B8860B);
                cropPaint.setStyle(Style.STROKE);
                int normalResId;
                int pressResId;
                if (mContext != null)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        final CropImage image = new CropImage();
                        if (i % 2 == 0)
                        {
                            normalResId = R.mipmap.rotate_horizontalrotate_a;
                            pressResId = R.mipmap.rotate_horizontalrotate_c;
                        }
                        else
                        {
                            normalResId = R.mipmap.rotate_verticalrotate_a;
                            pressResId = R.mipmap.rotate_verticalrotate_c;
                        }
                        image.bitmapNormal = BitmapFactory.decodeResource(
                                mContext.getResources(), normalResId);
                        image.bitmapPress = BitmapFactory.decodeResource(
                                mContext.getResources(), pressResId);
                        cropW = image.bitmapNormal.getWidth();
                        cropH = image.bitmapNormal.getHeight();
                        cropImages.add(image);
                    }
                    Matrix m = new Matrix();
                    m.setRotate(45);
                    final Bitmap tempNormal = cropImages.get(0).bitmapNormal;
                    final Bitmap tempPress = cropImages.get(0).bitmapPress;
                    final int w = tempNormal.getWidth();
                    final int h = tempNormal.getHeight();
                    Bitmap angles1 = Bitmap.createBitmap(tempNormal, 0, 0, w,
                            h, m, true);
                    Bitmap angles2 = Bitmap.createBitmap(tempPress, 0, 0, w, h,
                            m, true);
                    m.setRotate(-45);
                    Bitmap angles3 = Bitmap.createBitmap(tempNormal, 0, 0, w,
                            h, m, true);
                    Bitmap angles4 = Bitmap.createBitmap(tempPress, 0, 0, w, h,
                            m, true);
                    for (int i = 0; i < 4; i++)
                    {
                        final CropImage image = new CropImage();
                        if (i % 2 == 0)
                        {
                            image.bitmapNormal = angles1;
                            image.bitmapPress = angles2;
                        }
                        else
                        {
                            image.bitmapNormal = angles3;
                            image.bitmapPress = angles4;
                        }
                        cropImages.add(image);
                    }
                }
            }
            createCropBitmap();
            isCrop = true;
        }

        public void createCropBitmap()
        {
            w = mBitmap.getWidth();
            h = mBitmap.getHeight();
            cropBitmap = null;
            cropBitmap = Bitmap.createBitmap(w + CROP_EDGE + cropW, h
                    + CROP_EDGE + cropH, Config.ARGB_8888);
            cropBitmapW = cropBitmap.getWidth();
            cropBitmapH = cropBitmap.getHeight();
            Canvas mCanvas = new Canvas(cropBitmap);
            Rect r = new Rect(cropW / 2, cropH / 2, cropBitmap.getWidth()
                    - cropW / 2, cropBitmap.getHeight() - cropH / 2);
            mCanvas.drawRect(r, cropPaint);
            if (cropImages != null && cropImages.size() > 0)
            {
                final int cw = cropBitmap.getWidth();
                final int ch = cropBitmap.getHeight();
                CropImage ci = cropImages.get(0);
                ci.flag = CropImage.FLAG_LEFT;
                ci.xy[0] = 0;
                ci.xy[1] = (ch - cropH) / 2;
                ci.xy[2] = ci.bitmapNormal.getWidth();
                ci.xy[3] = ci.bitmapNormal.getHeight() + (ch - cropH) / 2;
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(1);
                ci.flag = CropImage.FLAG_TOP;
                ci.xy[0] = (cw - cropW) / 2;
                ci.xy[1] = 0;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight();
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(2);
                ci.flag = CropImage.FLAG_RIGHT;
                ci.xy[0] = cw - cropW;
                ci.xy[1] = (ch - cropH) / 2;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(3);
                ci.flag = CropImage.FLAG_BOTTOM;
                ci.xy[0] = (cw - cropH) / 2;
                ci.xy[1] = ch - cropH;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(4);
                ci.flag = CropImage.FLAG_LEFT_TOP;
                ci.xy[0] = -cropW / 4;
                ci.xy[1] = -cropH / 4;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(5);
                ci.flag = CropImage.FLAG_RIGHT_TOP;
                ci.xy[0] = cw - cropW * 5 / 4;
                ci.xy[1] = -cropH / 4;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(6);
                ci.flag = CropImage.FLAG_RIGHT_BOTTOM;
                ci.xy[0] = cw - cropW * 5 / 4;
                ci.xy[1] = ch - cropH * 5 / 4;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
                ci = cropImages.get(7);
                ci.flag = CropImage.FLAG_LEFT_BOTTOM;
                ci.xy[0] = -cropW / 4;
                ci.xy[1] = ch - cropH * 5 / 4;
                ci.xy[2] = ci.bitmapNormal.getWidth() + ci.xy[0];
                ci.xy[3] = ci.bitmapNormal.getHeight() + ci.xy[1];
                mCanvas.drawBitmap(getCropBitmap(ci), ci.xy[0], ci.xy[1], null);
            }
        }

        /**
         * 获取CropImage中的图片(正常的或是点击的图片)
         *
         * @param ci
         *            CropImage
         * @return Bitmap
         */
        private Bitmap getCropBitmap(CropImage ci)
        {
            return (ci.isEdit ? ci.bitmapPress : ci.bitmapNormal);
        }

        @Override
        public void reset()
        {
            isCrop = false;
        }

        public void resetEdit()
        {
            if (curCropImage != null)
            {
                curCropImage.isEdit = false;
                createCropBitmap();
            }
        }

        /**
         * 识别是否点击了边框按扭
         *
         * @param x
         *            画布的X坐标(注：画布不是本对象中的画布)
         * @param y
         *            画布的Y坐标(注：画布不是本对象中的画布)
         * @return true表示手指触摸的是边框上的按钮
         */
        public boolean edit(float x, float y)
        {
            int len = cropImages.size();
            boolean retVal = false;
            editFlag = CropImage.FLAG_NONE;
            final float innerX = x - (startX - CROP_EDGE / 2 - cropW / 2);
            final float innerY = y - (startY - CROP_EDGE / 2 - cropH / 2);
            for (int i = 0; i < len; i++)
            {
                if (innerX > 0 && innerY > 0)
                {
                    final CropImage ci = cropImages.get(i);
                    if (innerX > ci.xy[0] && innerY > ci.xy[1]
                            && innerX < ci.xy[2] && innerY < ci.xy[3])
                    {
                        editFlag = ci.flag;
                        ci.isEdit = true;
                        curCropImage = ci;
                        retVal = true;
                    }
                    else
                    {
                        ci.isEdit = false;
                    }
                }
            }
            return retVal;
        }

        /**
         * 判断点击区域是否在此图片内
         */
        @Override
        public boolean isInArea(float x, float y)
        {
            return x > startX && y > startY && x < (startX + w)
                    && y < (startY + h);
        }

        static class CropImage
        {
            public static final int FLAG_NONE = -1;

            public static final int FLAG_LEFT = 0;

            public static final int FLAG_TOP = 1;

            public static final int FLAG_RIGHT = 2;

            public static final int FLAG_BOTTOM = 3;

            public static final int FLAG_LEFT_TOP = 4;

            public static final int FLAG_RIGHT_TOP = 5;

            public static final int FLAG_LEFT_BOTTOM = 6;

            public static final int FLAG_RIGHT_BOTTOM = 7;

            public Bitmap bitmapNormal;

            public Bitmap bitmapPress;

            public float[] xy = new float[4];

            public int flag;

            public boolean isEdit;
        }
    }
}
